package com.yehuishou.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yehuishou.dao.SellItemDao;
import com.yehuishou.pojo.Address;
import com.yehuishou.pojo.Configurations;
import com.yehuishou.pojo.SellItemQueryParameters;
import com.yehuishou.pojo.ShoppingTrolley;
import com.yehuishou.pojo.User;
import com.yehuishou.service.SellItemService;
import com.yehuishou.service.UserService;

@Controller
public class SellItemController {
	@Resource
	private SellItemService sellitemservice;
	
	@Resource
	private SellItemDao sellitemdao;
	
	@Autowired
	private UserService userService;

	// 出售商品导航
	@RequestMapping("sellitemnavigation")
	public String sellItemNavigation(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "16") Integer pageSize, SellItemQueryParameters siqp,
			HttpServletRequest request) {
		Map<String, Object> map = sellitemservice.sellitemnavigation(siqp, pageNum, pageSize);
		request.setAttribute("map", map);
		request.setAttribute("siqp", siqp);
		return "sellitem_navigation";
	}

	// 出售商品详情
	@RequestMapping("sellitemparticulars")
	public String sellitem(SellItemQueryParameters siqp, HttpServletRequest request) {
		Map<String, Object> map = sellitemservice.sellitem(siqp);
		request.setAttribute("map", map);
		return "sellitem_particulars";
	}

	// 耶优品主页
	@RequestMapping("sellindex")
	public String sellindex(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "16") Integer pageSize, SellItemQueryParameters siqp,
			HttpServletRequest request) {
		// 品牌整机
		siqp.setCompletemachine(1);
		Map<String, Object> ppzj = sellitemservice.sellitemnavigation(siqp, pageNum, pageSize);
		// 游戏整机
		siqp.setCompletemachine(2);
		Map<String, Object> yxzj = sellitemservice.sellitemnavigation(siqp, pageNum, pageSize);
		// 新机上架
		siqp.setCompletemachine(3);
		Map<String, Object> newzj = sellitemservice.sellitemnavigation(siqp, pageNum, pageSize);
		request.setAttribute("ppzj", ppzj);
		request.setAttribute("yxzj", yxzj);
		request.setAttribute("newzj", newzj);
		return "computer";
	}
	
	
	//购买详情
	@RequestMapping("sellitemorder")
	public String sellitemorder(Integer[] stid,Integer[] i_id,Integer[] cfid,String[] title,Integer[] mun,String[] filePath,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		
		if(user!=null){
			Integer uid=user.getUid();
			List<ShoppingTrolley> shopts=new ArrayList<ShoppingTrolley>();
			for (int i = 0; i < i_id.length; i++) {
				SellItemQueryParameters siqp=new SellItemQueryParameters();
				siqp.setI_id(i_id[i]);
				siqp.setCfid(cfid[i]);
				List<Configurations> conf = sellitemdao.configurations(siqp);
				double payment=conf.get(0).getPrice()*mun[i];
				ShoppingTrolley shopt=new ShoppingTrolley();
				shopt.setI_id(i_id[i]);
				shopt.setCfid(cfid[i]);
				shopt.setTitle(title[i]);
				shopt.setFilePath(filePath[i]);
				shopt.setColormemory(conf.get(0).getColormemory());
				shopt.setPrice(conf.get(0).getPrice());
				shopt.setMun(mun[i]);
				shopt.setPayment(payment);
				shopts.add(shopt);
			}
			Address address = userService.findAByUid(uid);
			request.setAttribute("stid", stid);
			request.setAttribute("shopts", shopts);
			request.setAttribute("address", address);
			
			request.getSession().setAttribute("sellitemorder", "sellitemorder");
			return "sellitem_order";
		}
	
		return "redirect:user";
	}

}
